package com.sy.controller;

import com.sy.dto.CartItem;
import com.sy.dto.JsonResult;
import com.sy.dto.OrdersDto;
import com.sy.dto.SubjectDto;
import com.sy.entity.Subject;
import com.sy.mapper.OrdersMapper;
import com.sy.service.MemberCartService;
import com.sy.service.OrderService;
import com.sy.vo.CommitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * description: MemberCartController <br>
 * date: 2022/6/26 8:42 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */

@RestController
@RequestMapping("carts")
public class MemberCartController {
    @Autowired
    private MemberCartService memberCartService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrdersMapper ordersMapper;

    @RequestMapping("addItem")
    public void addItem(Integer subjectId, Long count, Integer memberId) {
        memberCartService.addItem(subjectId, count, memberId);
    }

    @RequestMapping("getCartInfo")
    public List<CartItem> getCartInfo(Integer memberId) {
        return memberCartService.getCartInfo(memberId);
    }

    @RequestMapping("delItem")
    public void delItem(Integer memberId, Integer subjectId) {
        memberCartService.deleteItem(memberId, subjectId);
    }

    @RequestMapping("updateCount")
    public void updateCount(Integer memberId, Integer subjectId, Long count) {
        memberCartService.updateCount(memberId, subjectId, count);
    }

    @RequestMapping("commit")
    public JsonResult commit(CommitVo commitVo){
        return new JsonResult().setCode(201).setData(orderService.submitOrder(commitVo.getSubjectId(),commitVo.getMemberId()));
    }

    @RequestMapping("getOrderById")
    public List<SubjectDto> getOrderById(Integer memberId){
        List<OrdersDto> ordersDtos = ordersMapper.selectMemberOrdersById(memberId);
        List<SubjectDto> subjectDtos =new ArrayList<>();
        for (OrdersDto ordersDto : ordersDtos) {
            subjectDtos.add(ordersDto.getSubject());
        }
        return subjectDtos;
    }
}
